package com.bbx.androidapi.secre;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.spec.InvalidKeySpecException;

import javax.crypto.Cipher;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;

public class DES {
	private static String Algorithm = "DESede";// 加密算法的名称
	private static Cipher c;// 密码器
	private static byte[] cipherByte;
	private static SecretKey deskey;// 密钥
	public static String keyString = "A3F2167DESJEIWBC9OTY45DYQWF68H1O";// 获得密钥的参数
																		// //对base64编码的string解码成byte数组

	public String getAuth(String key) {
		String dStr = "";
		DES des = new DES();
		try {
			des.getKey(keyString);
			byte[] dBy = des.deBase64(key);
			dStr = createDecryptor(dBy);
		} catch (InvalidKeyException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InvalidKeySpecException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (NoSuchPaddingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (NoSuchAlgorithmException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return dStr;
	}

	public byte[] deBase64(String parm) throws IOException {
		// Base64 dec = new Base64();
		byte[] dnParm = Base64.decode(parm);
		System.out.println(dnParm.length);
		System.out.println(dnParm);
		return dnParm;
	}

	// 把密钥参数转为byte数组
	public byte[] dBase64(String parm) throws IOException {
		// Base64 dec = new Base64();
		byte[] dnParm = Base64.decode(parm);
		return dnParm;
	}

	/**
	 * 对 Byte 数组进行解密
	 * 
	 * @param buff
	 *            要解密的数据
	 * @return 返回加密后的 String
	 */
	public String createDecryptor(byte[] buff) throws NoSuchPaddingException,
			NoSuchAlgorithmException, UnsupportedEncodingException {
		try {
			c.init(Cipher.DECRYPT_MODE, deskey);// 初始化密码器，用密钥deskey,进入解密模式
			cipherByte = c.doFinal(buff);
		} catch (java.security.InvalidKeyException ex) {
			ex.printStackTrace();
		} catch (javax.crypto.BadPaddingException ex) {
			ex.printStackTrace();
		} catch (javax.crypto.IllegalBlockSizeException ex) {
			ex.printStackTrace();
		}
		return (new String(cipherByte, "UTF-8"));
	}

	public void getKey(String key) throws IOException, InvalidKeyException,
			InvalidKeySpecException {
		byte[] dKey = dBase64(key);
		try {
			deskey = new javax.crypto.spec.SecretKeySpec(dKey, Algorithm);
			c = Cipher.getInstance(Algorithm);
		} catch (NoSuchPaddingException ex) {
		} catch (NoSuchAlgorithmException ex) {
		}
	}

	public static String Des(String tmp) {
		String dStr = "";
		try {
			DES des = new DES();
			des.getKey(DES.keyString);
			byte[] dBy = des.deBase64(tmp);
			dStr = des.createDecryptor(dBy);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			// Utils.printDebugInfo(" Base64 解密出错");
		}
		return dStr;
	}

	// public static void main(String args[]) throws IOException,
	// NoSuchAlgorithmException, NoSuchPaddingException,
	// InvalidKeySpecException, InvalidKeyException, IOException {
	// SecreKeyUtils des = new SecreKeyUtils();
	// des.getKey(keyString);
	// byte[] dBy = des.deBase64("bm3Z0Ccexetpai6jqhPmkLSIJFcne3+0");
	// String dStr = des.createDecryptor(dBy);
	// System.out.println("解：" + dStr);
	// }

}
